package com.jinlejia.bbs.logic;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/3/29.
 */
//回答话题
public class UploadAnswerLogic {
    private Context context;

    public UploadAnswerLogic(Context context) {
        this.context = context;
    }

    public void execute(boolean isInvite,String topicCode, InformationBean bean, final LogicCallBack callBack) {
        JSONObject json = new JSONObject();
        String content = null;
        if (bean.getSoundContent() != null) {
            json.put("contentType", 2);
            content = bean.getSoundContent().toString();
        } else if (bean.getMessageContent() != null) {
            json.put("contentType", 1);
            content = bean.getMessageContent();
        }
        if (isInvite){
            json.put("isInvite",1);
        }else {
            json.put("isInvite",2);
        }
        json.put("content", content);
        json.put("topicCode", topicCode);
        httpCallForm(BBSConfig.saveAnswer, json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        boolean flag = parse.parse(Boolean.class);
                        callBack.onResult(flag);
                    }

                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false);
                    }
                }));
    }

    public interface LogicCallBack {
        void onResult(boolean flag);
    }
}
